// gorpc/codec/protobuf.go

package codec

import (
	"github.com/golang/protobuf/proto"
	"gorpc/common"
)

type ProtobufCodec struct{}

func (c *ProtobufCodec) Encode(v interface{}) ([]byte, error) {
	msg, ok := v.(proto.Message)
	if !ok {
		return nil, common.NewRPCError(
			common.ErrorCodeSerialization,
			"protobuf requires proto.Message",
			nil)
	}
	return proto.Marshal(msg)
}

func (c *ProtobufCodec) Decode(data []byte, v interface{}) error {
	return proto.Unmarshal(data, v.(proto.Message))
}
